//
//  HomeViewCell.swift
//  SinaWeiBo
//
//  Created by 有风 on 16/2/20.
//  Copyright © 2016年 YouFeng. All rights reserved.
//

import UIKit
import SnapKit

class HomeViewCell: UITableViewCell {

    var bottomViewTopConstaint: Constraint?

    
    var status: Status? {
        didSet {
            homeCellOriginalView.status = status
            //卸载约束对象
            self.bottomViewTopConstaint?.uninstall()
            //需要根据数据来调整约束关系
            if let re = status?.retweeted_status {
                //转发微博
                
                //给转发微博设置数据模型
                homeCellRetweetedView.retweetedStatus = re
                homeCellBottomView.snp_makeConstraints { (make) -> Void in
                    //添加约束对象 并且记录约束对象
                    self.bottomViewTopConstaint =  make.top.equalTo(homeCellRetweetedView.snp_bottom).constraint
                }
                
                //显示转发微博视图  不能够被计算为 CGSizeZero  如果不做隐藏操作 会导致数据显示错乱
                homeCellRetweetedView.hidden = false
                
            } else {
                //原创微博
                homeCellBottomView.snp_makeConstraints { (make) -> Void in
                    //添加约束对象 并且记录约束对象
                    self.bottomViewTopConstaint =  make.top.equalTo(homeCellOriginalView.snp_bottom).constraint
                }
                //隐藏转发微博视图
                homeCellRetweetedView.hidden = true
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(homeCellOriginalView)
        contentView.addSubview(homeCellBottomView)
        contentView.addSubview(homeCellRetweetedView)

        
        
        
        
        //
        homeCellOriginalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            make.left.equalTo(contentView.snp_left)
            
            
        }
        
        homeCellRetweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(homeCellOriginalView.snp_bottom)
            make.left.equalTo(homeCellOriginalView.snp_left)
            make.right.equalTo(homeCellOriginalView.snp_right)
        }

        
        homeCellBottomView.snp_makeConstraints { (make) -> Void in
            //make.top.equalTo(homeCellRetweetedView.snp_bottom).constraint
            make.right.equalTo(contentView.snp_right)
            make.left.equalTo(contentView.snp_left)
            make.height.equalTo(35)
            

        }
        
        //给contentView添加约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            //最关键的约束
            //make.height.equalTo(300)
            make.bottom.equalTo(homeCellBottomView.snp_bottom)
            
        }

        
        
    }
    
    
    
    
    
    private lazy var homeCellRetweetedView: HomeCellRetweetedView = HomeCellRetweetedView()

    private lazy var homeCellOriginalView: HomeCellOriginalView = HomeCellOriginalView()
    private lazy var homeCellBottomView: HomeCellBottomView = HomeCellBottomView()
}
